package com.project.util;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

public class EhcacheUtil {
	
	private static CacheManager manager = CacheManager.create();
	
//	private static EhcacheUtil ehCache;
//	
//	private EhcacheUtil() {  
//        manager = CacheManager.create();  
//    }  
//	  
//    public static EhcacheUtil getInstance() {  
//        if (ehCache== null) {  
//            ehCache= new EhcacheUtil();  
//        }  
//        return ehCache;  
//    }
	
	public static void newCache(String name, int maxElementsInMemory, boolean overflowToDisk, boolean eternal, long timeToLiveSeconds,
			long timeToIdleSeconds){
		Cache cache = new Cache(name, maxElementsInMemory, overflowToDisk, eternal, timeToLiveSeconds, timeToIdleSeconds);
		manager.addCache(cache);
	}
    
    public static void put(String cacheName, String key, Object value) {  
        Cache cache = manager.getCache(cacheName);  
        Element element = new Element(key, value);  
        cache.put(element);  
    }  
  
    public static Object get(String cacheName, String key) {  
        Cache cache = manager.getCache(cacheName);  
        Element element = cache.get(key);  
        return element == null ? null : element.getObjectValue();  
    }  
  
    public static Cache get(String cacheName) {  
        return manager.getCache(cacheName);  
    }  
  
    public static void remove(String cacheName, String key) {  
        Cache cache = manager.getCache(cacheName);  
        cache.remove(key);  
    }  
	

}
